package gui;

import jahuwaldt.plot.Plot;
import jahuwaldt.plot.Plot2D;
import jahuwaldt.plot.PlotPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;

public class InternalPlotPane extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 110688110130127238L;

	public InternalPlotPane(String title) {
		super();
		this.initialize(title);
	}

	public InternalPlotPane(Plot aPlot) {
		super();
		this.plot = aPlot;
		this.initialize(aPlot.getTitle());

	}

	public InternalPlotPane(File file) {
		super();
		this.file = file;
		this.initialize(this.file.getPath());
		this.readFile();

	}

	public void setText(String text) {

	}

	private void readFile() {
		String read = "";
		if (file != null) {
			try {
				FileReader reader = new FileReader(file);
				BufferedReader bufreader = new BufferedReader(reader);
				while ((read = bufreader.readLine()) != null) {
					fileContent += read + "\n";
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	private void initialize(String title) {
		this.setTitle(title);
		this.setResizable(true);
		this.setClosable(true);
		this.setMaximizable(true);
		this.setIconifiable(true);
		this.setSize(300, 300);
		this.setLocation(20, 20);
		this.setVisible(true);
		// DefaultSyntaxKit.initKit();
		// this.textArea = new JEditorPane();
		// this.editorPane = new JEditorPane();
		initPlot();

		this.scrollPane = new JScrollPane(this.plotPanel);
		// this.textArea.setContentType("text/java");
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(this.scrollPane);
		this.getContentPane().doLayout();
	}

	private void initPlot() {
		/*
		 * Plot2D aPlot = new SimplePlotXY(xArr, yArr, "Test SimpleXY Plot",
		 * "X Axis", "Y Axis", null, null, new CircleSymbol());
		 */

		Plot2D aPlot = (Plot2D) this.plot;

		// Make the horizontal axis a log axis.
		//PlotAxis xAxis = aPlot.getHorizontalAxis();
		//xAxis.setScale(new Log10AxisScale());

		// Create a 2nd run and add it to the plot.
		//PlotRunList runs = aPlot.getRuns();
		//runs.add(new PlotRun(xArr, yArrAlt, true, new SquareSymbol()));

		this.plotPanel = new PlotPanel(aPlot);
		plotPanel.setBackground(Color.white);
	}

	public File getFile() {
		return this.file;
	}

	public void saveFile() {
		if (this.file != null) {
			try {
				saveWriter = new FileWriter(file);

				saveWriter.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void saveAs(File file) {
		try {
			saveWriter = new FileWriter(file);

			saveWriter.flush();
			this.file = file;
			this.setTitle(file.getPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setPlot(Plot plot) {
		this.plot = plot;
	}

	public Plot getPlot() {
		return plot;
	}

	private PlotPanel plotPanel = null;

	private JScrollPane scrollPane = null;
	private File file = null;
	private String fileContent = "";
	private FileWriter saveWriter = null;
	private Plot plot = null;
}
